<template>
    <Modal :title="title" v-model="show" :scrollable="false" :mask-closable="false" width="500">
        <Form :model="form" ref="form" :rules="rules" :label-width="120">
            <Row>
                <i-col>
                    <FormItem label="新密码：" prop="loginPwd" style="width: 80%">
                        <Input clearable type="password" name="loginPwd" placeholder="请输入密码" v-model="form.loginPwd"/>
                    </FormItem>
                    <FormItem label="确认密码：" prop="confirmLoginPwd" style="width: 80%">
                        <Input clearable type="password" name="confirmLoginPwd" placeholder="请确认密码" v-model="form.confirmLoginPwd"/>
                    </FormItem>
                </i-col>
            </Row>
        </Form>
        <div slot="footer" style="text-align: right">
            <Button type="primary" @click="doSubmit()">确定</Button>
        </div>
    </Modal>
</template>

<script>
    export default {
        data () {
            return {
                title: '修改密码',
                show: false,
                form: {
                    loginPwd: null,
                    confirmLoginPwd: null
                },
                rules: {
                    loginPwd: [
                        {required: true, type: "string", message: '请输入密码', trigger: 'blur'},
                    ],
                    confirmLoginPwd: [
                        {required: true, type: "string", message: '请确认密码', trigger: 'blur'},
                    ]
                }
            };
        },
        created(){},
        methods: {
            doSubmit(){
                const that = this;
                this.$refs['form'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    if(that.form.loginPwd != that.form.confirmLoginPwd){
                        that.$Message.error({content: '两次密码不一致'});
                        return;
                    }
                    that.$post("/api/user/updatePwd", {loginPwd:that.form.loginPwd}).then(res => {
                        if (res.code != 0) {
                            that.$Message.error({content: res.errmsg ? res.errmsg : that.title + '失败'});
                            return;
                        }
                        that.show = false;
                        that.$Message.success({content:  '修改密码成功,需重新登录', duration: 3});

                        // 修改密码后跳转登录页
                        that.$router.push({path: '/login'});
                    }).catch(res => {
                        that.$Message.error({content: '修改密码失败：' + res});
                    });
                })
            },
            showDialog(){
                this.show = true;
            }
        },
        computed: {},
        watch: {}
    }
</script>

<style>

</style>
